---
title: "Subgroup analysis: information seeking"
author: "[redacted]"
date: "2025-04-07"
output: html_document
---

```{r}
```

#####LOAD PACKAGES AND DATA

```{r}
#Load packages
library(tidyverse)
library(cjoint)
library(cregg)

#Load data
conjoint_info <- cjoint::read.qualtrics(
  "data_clean.csv",
  covariates = c("information_seeking"),
  respondentID = "ResponseId",
  letter = "F",
  new.format = TRUE,
  responses = paste0("conjoint_", c("intro", 1:8))
)

#Remove NAs
conjoint_info <- conjoint_info %>% drop_na(selected)

names(conjoint_info)

```

#RECODE COVARIATE

```{r}

conjoint_info$information_seeking_num <- as.numeric(as.character(conjoint_info$information_seeking))

mean(conjoint_info$information_seeking_num, na.rm = TRUE)

conjoint_info <- conjoint_info %>%
  mutate(
    inf_cat = case_when(
      information_seeking_num %in% c(2,3,4) ~ "low",    # Levels 2, 3, 4 recoded as "low"
      information_seeking_num %in% c(0,1) ~ "high",   # Levels 0, 1 recoded as "high"
      TRUE ~ NA_character_                  # Handle any unexpected values with NA
    ),
    inf_cat = factor(inf_cat, levels = c("low", "high"))
  )

summary(conjoint_info$inf_cat)

```

#RENAME VARIABLES

```{r}

# Renaming attributes and levels in English
conjoint_info <-  conjoint_info %>% dplyr::rename(Topic=Onderwerp, 
                                            Relation.to.you=Relatie.tot.u,
                                            Apparent.knowledge.of.the.person=Schijnbare.kennis.van.de.persoon,
                                            Apparent.concern.of.the.person=Schijnbare.bezorgdheid.van.de.persoon,
                                            Apparent.motivation.of.the.person=Schijnbare.motivatie.van.de.persoon,
                                            Closeness.with.you=Band.met.u,
                                            Gender.of.the.person=Geslacht.van.de.persoon
                                            ) %>%
                                     mutate(
                                            Topic = recode_factor(Topic,
                                                  "overstromingen" = "floods", 
                                                  "besmettelijke ziekten" = "infectious diseases",
                                                  "pesticiden" = "pesticides",
                                                  "levensstijlziekten" = "lifestyle diseases",),
                                            Gender.of.the.person = recode_factor(Gender.of.the.person,
                                                  "vrouw" = "female", 
                                                  "man" = "male"),
                                            Relation.to.you = recode_factor(Relation.to.you,
                                                  "vriend/vriendin (platonisch)" = "friend",
                                                  "collega/studiegenoot" = "colleague/fellow student",
                                                  "familielid" = "relative"),
                                            Closeness.with.you = recode_factor(Closeness.with.you,
                                                  "zeer hecht" = "very close", 
                                                  "redelijk hecht" = "fairly close",
                                                  "enigszins hecht" = "somewhat close",
                                                  "niet hecht" = "not close"),
                                            Apparent.knowledge.of.the.person = recode_factor(Apparent.knowledge.of.the.person,
                                                  "zeer goed geinformeerd" = "very well-informed", 
                                                  "redelijk goed geinformeerd" = "fairly well-informed",
                                                  "enigszins geinformeerd" = "somewhat well-informed",
                                                  "niet geinformeerd" = "not well-informed"),
                                            Apparent.concern.of.the.person = recode_factor(Apparent.concern.of.the.person,
                                                  "zeer bezorgd" = "very concerned", 
                                                  "redelijk bezorgd" = "fairly concerned",
                                                  "enigszins bezorgd" = "somewhat concerned",
                                                  "niet bezorgd" = "not concerned"),
                                            Apparent.motivation.of.the.person = recode_factor(Apparent.motivation.of.the.person,
                                                  "u te overtuigen om meer maatregelen te nemen" = "to convince you to take more precautions", 
                                                  "informatie over het risico met u te willen uitwisselen" = "to exchange information with you",
                                                  "zich minder ongerust voelen" = "to feel less anxious",
                                                  "u te overtuigen om minder maatregelen te nemen" = "to convince you to take fewer precautions"))


#Check renamings
table(conjoint_info$Topic)
table(conjoint_info$Gender.of.the.person)
table(conjoint_info$Relation.to.you)
table(conjoint_info$Closeness.with.you)
table(conjoint_info$Apparent.knowledge.of.the.person)
table(conjoint_info$Apparent.concern.of.the.person)
table(conjoint_info$Apparent.motivation.of.the.person)


```

#CHANGE VARIABLE ORDER AND SET REFERENCE CATEGORIES

```{r}

#REORDER LEVELS
conjoint_info$Topic <-                             factor(conjoint_info$Topic,
                                                  levels = c("pesticides", "floods", "lifestyle diseases", "infectious diseases"))
conjoint_info$Gender.of.the.person <-              factor(conjoint_info$Gender.of.the.person,
                                                  levels = c(  "male", "female"))
conjoint_info$Relation.to.you <-                   factor(conjoint_info$Relation.to.you,
                                                  levels = c("colleague/fellow student", "friend", "relative"))
conjoint_info$Closeness.with.you <-                factor(conjoint_info$Closeness.with.you,
                                                  levels = c("not close", "somewhat close", "fairly close", "very close"))
conjoint_info$Apparent.knowledge.of.the.person <-  factor(conjoint_info$Apparent.knowledge.of.the.person,
                                                  levels = c("not well-informed", "somewhat well-informed", "fairly well-informed", "very well-informed"))
conjoint_info$Apparent.concern.of.the.person <-    factor(conjoint_info$Apparent.concern.of.the.person,
                                                  levels = c("not concerned", "somewhat concerned", "fairly concerned", "very concerned"))
conjoint_info$Apparent.motivation.of.the.person <- factor(conjoint_info$Apparent.motivation.of.the.person,
                                                  levels = c("to convince you to take more precautions", "to convince you to take fewer precautions", "to feel less anxious", "to exchange information with you"))


```

######RUN MAIN AMCE, MM, AND DIFFERENCES IN MM

```{r}
###AMCE

#Full model
amce_inf <- cj(conjoint_info, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ inf_cat, 
                  id = ~ Response.ID,
                  estimate = "amce")


# Define the dodge width to create separation between low and high information seeking lines
dodge_width <- 1

amce_inf_plot <- ggplot(amce_inf, aes(x = estimate, y = level, color = inf_cat, shape = inf_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("chartreuse3", "darkgreen")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes by information seeking", color = "Information seeking", shape = "Information seeking") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/information_seeking/amce_inf.png", plot = amce_inf_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_inf <- cj(conjoint_info, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ inf_cat, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_inf$feature <- factor(mm_inf$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high information seeking lines
dodge_width <- 1

mm_inf_plot <- ggplot(mm_inf, aes(x = estimate, y = level, color = inf_cat, shape = inf_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0.5, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("chartreuse3", "darkgreen")) +  # Custom colors for gender_cat
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by information seeking", color = "Information seeking", shape = "Information seeking") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)


ggsave("figures/information_seeking/mm_inf.png", plot = mm_inf_plot, width = 8, height = 6, dpi = 300)


###DIFFERENCE IN MARGINAL MEANS

diff_inf <- cj(conjoint_info, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ inf_cat, 
                  id = ~ Response.ID,
                  estimate = "mm_differences")


# Define the dodge width to create separation between low and high information seeking lines
dodge_width <- 1

diff_inf_plot <- ggplot(diff_inf, aes(x = estimate, y = level, color = inf_cat, shape = inf_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkgreen")) + 
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Difference in Marginal Means", y = "All attributes by information seeking", color = "Information seeking", shape = "Information seeking") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.1, 0.1)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/information_seeking/diff_inf.png", plot = diff_inf_plot, width = 8, height = 6, dpi = 300)




```

#DIAGNOSTICS

```{r}

#BALANCE TESTING

# Manually map gender_cat to numeric (assuming "low" and "high")
conjoint_info$inf_cat_num <- ifelse(conjoint_info$inf_cat == "low", 0, 
                                      ifelse(conjoint_info$inf_cat == "high", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_info$inf_cat_num)

# Calculate marginal means using your dataset and attributes
infbalance <- plot(mm(conjoint_info, inf_cat_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 1), vline = mean(conjoint_info$inf_cat_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
infbalance_adj <- infbalance +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: Information Seeking Behavior") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_info.png", plot = infbalance_adj, width = 15, height = 11, dpi = 300)

```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
